package com.qumitech.visualnovel.service.drift.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.thirdapi.aliyun.AliYunOSSUtil;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.drift.DriftBottleAdminDTO;
import com.qumitech.visualnovel.pubdef.pubenum.AdminStateEnum;
import com.qumitech.visualnovel.service.drift.DriftBottleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ywxie
 * @date 2019/9/24 17:05
 * @description
 */
@Service
public class DriftBottleServiceImpl implements DriftBottleService {

    private final String DRIFT_BOTTLE_ADMIN_URL = "/admin/drift";

    @Override
    public Map<String, Object> listDriftBottleByConditions(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + DRIFT_BOTTLE_ADMIN_URL;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void updateDriftBottleAdmin(DriftBottleAdminDTO dto) {
        dto.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + DRIFT_BOTTLE_ADMIN_URL;
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        if ((dto.getResourceUrl() != null && !dto.getResourceUrl().isEmpty()) && dto.getAuditState().equals(AdminStateEnum.DELETED.value())){
            List<String> newUrlList = new ArrayList<>();
            for (String voiceUrl : dto.getResourceUrl()) {
                newUrlList.add(voiceUrl.substring(0, voiceUrl.indexOf("_DURATION")));
            }

            if (newUrlList != null && !newUrlList.isEmpty()){
                AliYunOSSUtil.deleteObjectList(newUrlList);
            }
        }
    }

    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
